<?php

class Ialbum_Api_Content extends Core_Api_Abstract {
	
    protected $_left_col = array();
    protected $_mid_col = array();
    protected $_right_col = array();
	private $photo_id = 0;


	public function setPhotoId($photo_id){
		$this->photo_id = $photo_id;
	}
	public function getPhotoId(){
		return $this->photo_id;
	}
	//THIS FUNCTION USE TO SET EACH LAYOUT
    public function set($layout,$direction) {
        if (is_array($layout)) {
            foreach ($layout as $layout_element) {
                if (is_array($layout_element)) {
                    switch ($direction) {
                        case 'left':
                            $this->_left_col[] = $layout_element;
                            break;
                        case 'mid':
                            $this->_mid_col[] = $layout_element;
                            break;
                        case 'right':
                            $this->_right_col[] = $layout_element;
                            break;
                    }

                }
                elseif (is_string($layout_element)) {

                    switch ($direction) {
                        case 'left':
                            $this->_left_col[] = array('type' => 'widget','name' => $layout_element);
                            break;
                        case 'mid':
                            $this->_mid_col[] = array('type' => 'widget','name' => $layout_element);
                            break;
                        case 'right':
                            $this->_right_col[] = array('type' => 'widget','name' => $layout_element);
                            break;
                    }

                }
                else {
                    throw new Core_Model_Exception("Layout each LEFT input params element must be an array");
                }
            }

        }
        elseif (is_string($layout)) {
            $this->_left_col[] = array('type' => 'widget','name' => $layout);
            switch ($direction) {
                case 'left':
                    $this->_left_col[] = array('type' => 'widget','name' => $layout);
                    break;
                case 'mid':
                    $this->_mid_col[] = array('type' => 'widget','name' => $layout);
                    break;
                case 'right':
                    $this->_right_col[] = array('type' => 'widget','name' => $layout);
                    break;
            }
        }
        else {
            throw new Core_Model_Exception("Layout LEFT input params must be an array");
        }
        return $this;
    }


    public function setLeftLayout($layout) {
        $this->set($layout,'left');
        return $this;
    }

    public function setMidLayout($layout) {
        $this->set($layout,'mid');
        return $this;
    }

    public function setRightLayout($layout) {
        $this->set($layout,'right');
        return $this;
    }


    public function setLayout($layout) {
        if (!is_array($layout)) {
            if (isset($layout['right'])) $this->setRightLayout($layout['right']);
            if (isset($layout['middle'])) $this->setMidLayout($layout['middle']);
            if (isset($layout['left'])) $this->setLeftLayout($layout['left']);

        }
        else {
            throw new Core_Model_Exception("Layout input params must be an array");
        }
        return $this;
    }




    public function renderLeft() {
        $render = '';
        foreach($this->_left_col as $widget) {
            $element = new Engine_Content_Element_Container(array(
                            'elements' => array($widget),
                            'decorators' => array(
                                    'Children'
                            )
            ));
            $render .= $element->render();
        }
        return $render;

    }

    public function renderMid() {
        $render = '';
        foreach($this->_mid_col as $widget) {
            $element = new Engine_Content_Element_Container(array(
                            'elements' => array($widget),
                            'decorators' => array(
                                    'Children'
                            )
            ));
            $render .= $element->render();
        }
        return $render;

    }

    public function renderRight() {
        $render = '';
        foreach($this->_right_col as $widget) {
            $element = new Engine_Content_Element_Container(array(
                            'elements' => array($widget),
                            'decorators' => array(
                                    'Children'
                            )
            ));
            $render .= $element->render();
        }
        return $render;

    }

    public function clearLayout() {
        unset($this->_left_col);
        unset($this->_mid_col);
        unset($this->_right_col);

        $this->_left_col = array();
        $this->_right_col = array();
        $this->_mid_col = array();
        return $this;
    }

    public function isLeftExist() {
        if ($this->_left_col)
            return true;
        else
            return false;
    }
    public function isMidExist() {
        if ($this->_mid_col)
            return true;
        else
            return false;
    }
    public function isRightExist() {
        if ($this->_right_col)
            return true;
        else
            return false;
    }
    public function getWidgetsLeft() {
        return $this->setLeftLayout(array(
             array(
                )
                ))->renderLeft();
    }
    public function getWidgetCommentPhoto() {
        return $this->setRightLayout(array(
             array(
                        'type' => 'widget',
                        'name' => 'ialbum.comment-photo'
                )
                ))->renderRight();
    }
	public function getWidgetComment() {
        return $this->setMidLayout(array(
             array(
                        'type' => 'widget',
                        'name' => 'ialbum.comment'
                )
                ))->renderMid();
    }
}